# TODO: Add comment
# 
# Author: Forever
###############################################################################
library(hydroGOF)
library(clusterSim)

data_folder = "MYD30"



# Ha Noi
station = "1"

result = data.frame()

inputPath = paste("C:/Users/Forever/Desktop/",data_folder,"/",station,"_time.csv",sep="")
data=read.csv(inputPath)

mua_mua = subset(data,aqsmonth>=5&aqsmonth<=9)
mua_kho = subset(data,aqsmonth>=11|aqsmonth<=3)


model_re = sum(abs(mua_mua$avg_pm-mua_mua$ukColumn)/mua_mua$avg_pm)/nrow(mua_mua)*100
model_rmse = rmse(mua_mua$avg_pm,mua_mua$ukColumn)
model_r = cor(mua_mua$avg_pm,mua_mua$ukColumn)

samples = nrow(mua_mua)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)
result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


model_re = sum(abs(mua_kho$avg_pm-mua_kho$ukColumn)/mua_kho$avg_pm)/nrow(mua_kho)*100
model_rmse = rmse(mua_kho$avg_pm,mua_kho$ukColumn)
model_r = cor(mua_kho$avg_pm,mua_kho$ukColumn)

samples = nrow(mua_kho)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)

result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


#HUE

station = "2"

inputPath = paste("C:/Users/Forever/Desktop/",data_folder,"/",station,"_time.csv",sep="")
data=read.csv(inputPath)

mua_mua =  subset(data,aqsmonth>=11|aqsmonth<=1)
mua_kho = subset(data,aqsmonth>=2&aqsmonth<=10)


model_re = sum(abs(mua_mua$avg_pm-mua_mua$ukColumn)/mua_mua$avg_pm)/nrow(mua_mua)*100
model_rmse = rmse(mua_mua$avg_pm,mua_mua$ukColumn)
model_r = cor(mua_mua$avg_pm,mua_mua$ukColumn)

samples = nrow(mua_mua)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)
result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


model_re = sum(abs(mua_kho$avg_pm-mua_kho$ukColumn)/mua_kho$avg_pm)/nrow(mua_kho)*100
model_rmse = rmse(mua_kho$avg_pm,mua_kho$ukColumn)
model_r = cor(mua_kho$avg_pm,mua_kho$ukColumn)

samples = nrow(mua_kho)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)

result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))

#PHU THO

station = "4"

inputPath = paste("C:/Users/Forever/Desktop/",data_folder,"/",station,"_time.csv",sep="")
data=read.csv(inputPath)

mua_mua = subset(data,aqsmonth>=5&aqsmonth<=9)
mua_kho = subset(data,aqsmonth>=11|aqsmonth<=3)


model_re = sum(abs(mua_mua$avg_pm-mua_mua$ukColumn)/mua_mua$avg_pm)/nrow(mua_mua)*100
model_rmse = rmse(mua_mua$avg_pm,mua_mua$ukColumn)
model_r = cor(mua_mua$avg_pm,mua_mua$ukColumn)

samples = nrow(mua_mua)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)
result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


model_re = sum(abs(mua_kho$avg_pm-mua_kho$ukColumn)/mua_kho$avg_pm)/nrow(mua_kho)*100
model_rmse = rmse(mua_kho$avg_pm,mua_kho$ukColumn)
model_r = cor(mua_kho$avg_pm,mua_kho$ukColumn)

samples = nrow(mua_kho)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)

result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


#DA NANG

station = "5"

inputPath = paste("C:/Users/Forever/Desktop/",data_folder,"/",station,"_time.csv",sep="")
data=read.csv(inputPath)

mua_mua = subset(data,aqsmonth>=9&aqsmonth<=12)
mua_kho = subset(data,aqsmonth>=1&aqsmonth<=8)


model_re = sum(abs(mua_mua$avg_pm-mua_mua$ukColumn)/mua_mua$avg_pm)/nrow(mua_mua)*100
model_rmse = rmse(mua_mua$avg_pm,mua_mua$ukColumn)
model_r = cor(mua_mua$avg_pm,mua_mua$ukColumn)

samples = nrow(mua_mua)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)
result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


model_re = sum(abs(mua_kho$avg_pm-mua_kho$ukColumn)/mua_kho$avg_pm)/nrow(mua_kho)*100
model_rmse = rmse(mua_kho$avg_pm,mua_kho$ukColumn)
model_r = cor(mua_kho$avg_pm,mua_kho$ukColumn)

samples = nrow(mua_kho)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)

result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


#HA LONG

station = "6"

inputPath = paste("C:/Users/Forever/Desktop/",data_folder,"/",station,"_time.csv",sep="")
data=read.csv(inputPath)

mua_mua = subset(data,aqsmonth>=5&aqsmonth<=9)
mua_kho = subset(data,aqsmonth>=11|aqsmonth<=3)


model_re = sum(abs(mua_mua$avg_pm-mua_mua$ukColumn)/mua_mua$avg_pm)/nrow(mua_mua)*100
model_rmse = rmse(mua_mua$avg_pm,mua_mua$ukColumn)
model_r = cor(mua_mua$avg_pm,mua_mua$ukColumn)

samples = nrow(mua_mua)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)
result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))


model_re = sum(abs(mua_kho$avg_pm-mua_kho$ukColumn)/mua_kho$avg_pm)/nrow(mua_kho)*100
model_rmse = rmse(mua_kho$avg_pm,mua_kho$ukColumn)
model_r = cor(mua_kho$avg_pm,mua_kho$ukColumn)

samples = nrow(mua_kho)
r2= round(model_r*model_r,3)
rmse = round(model_rmse,3)
re = round(model_re,3)

result= rbind(result,data.frame(data_folder,station,samples,r2,rmse,re))



outPath = paste("C:/Users/Forever/Desktop/out/",data_folder,"/season.csv",sep="")
write.csv(result,outPath)